package docks;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.jdom.Element;

import utils.Coordinates;
import utils.HTML;
import xmlparser.JdomParser;

public class DockAPIImpl implements DockAPI {

	String _xmlName;
	
	public DockAPIImpl(String URL, String xmlName) {
		
		HTML.extractAndSaveXML(HTML.getHTML(URL), "<Document>", "</Document>", xmlName);
		_xmlName = xmlName;
	}
	
	public List<Dock> getAllDocks() {
		
	    	StringTokenizer st, staddress;
	    	Dock dock;
	    	List<Dock> result = new ArrayList<Dock>();
		JdomParser jp = new JdomParser(_xmlName);
		List<String> list = jp.parseDocument();
		
		for (String s : list) {
		    
		    dock = new Dock();
		    st = new StringTokenizer(s);
		    
		    String address = st.nextToken(":");
		    staddress = new StringTokenizer(address);
		    
		    dock.setIdDock(Integer.valueOf(staddress.nextToken("-").trim()));
		    dock.setAddress(staddress.nextToken("-").trim());
		    dock.setFreeSlots(Integer.valueOf(st.nextToken(":")));
		    dock.setAvailableBikes(Integer.valueOf(st.nextToken(":")));
		    dock.setLongitude(Double.valueOf(st.nextToken(":")));
		    dock.setLatitude(Double.valueOf(st.nextToken(":")));
		    result.add(dock);
		}
		
		return result;
	}

	@Override
	public List<Dock> getCloseDocks() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Dock getDockInfo(Coordinates coord) {
		
//		JdomParser jp = new JdomParser(_xmlName);
//		List<String> list = jp.parseDocument();
//		for (String s : list) {
//		    
//		    System.out.println(s);
//		}
	    	List<Dock> list = getAllDocks();
	    	
	    	for (Dock d : list) {
	    	    
	    	    System.out.println("id: " + d.getIdDock());
	    	    System.out.println("address: " + d.getAddress());
	    	    System.out.println("free: " + d.getFreeSlots());
	    	    System.out.println("available: " + d.getAvailableBikes());
	    	    System.out.println("latitude: " + d.getLatitude());
	    	    System.out.println("longitude: " + d.getLongitude());
	    	    System.out.println("\n ---------------- \n");
	    	}
	    	
		return null;
	}

	@Override
	public Dock getDockInfo(int dockid) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
